const Util = require('../../utils/util.js');
const Api = require('../../api/api.js')
const app = getApp()
Page({
  data: {
    orderId: '',
    orderRetreat: '',
    items: {
      amount: '',
      assistCheckNo: '',
      bookType: '',
      buyerId: '',
      corpCode: '',
      createBy: '',
      createTime: '',
      deleted: '',
      finalSum: '',
      goodsCode: '',
      goodsImgUrl: '',
      goodsName: '',
      id: '',
      infov: '',
      isSend: '',
      isSync: '',
      linkIdcard: '',
      linkMobile: '',
      linkName: '',
      modifyBy: '',
      modifyTime: '',
      occDate: '',
      orderNo: '',
      orderStatus: '',
      orderType: '',
      outOrderNo: '',
      payStatus: '',
      paySum: '',
      price: '',
      subOrderNo: '',
      totalPrice: '',
      zybOrderType: ''
    }

  },
  onLoad: function (paras) {
    console.log('orderdetail' + paras)
    this.setData({
      orderId: paras.orderid
    })
    this.bindData()
  },
  // 取消订单
  cancelClick: function (event) {
    var pagethis = this
    wx.showModal({
      title: '订单取消',
      content: '确定取消该订单？',
      success: function (rps) {
        if (rps.confirm) {
          Api.cancelOrder({
            data: app.addParams({
              orderId: event.target.dataset.id,
            }),
            success: (res) => {
              let _items = pagethis.data.items;
              _items.payStatus = 'cancel_pay';
              _items.payStatusName = '交易撤销';
              pagethis.setData({
                items: _items
              })
            }
          })
        }

      }
    })

  },
  // 支付
  payClick: function (event) {
    console.log("to orderpay rps:" + event.target.dataset.id);
    var pagethis = this
    Api.toWxPay({
      data: app.addParams({
        orderId: event.target.dataset.id
      }),
      success: (rps) => {
        wx.requestPayment({
          'timeStamp': rps.data.data.timeStamp,
          'nonceStr': rps.data.data.nonceStr,
          'package': rps.data.data.packageStr,
          'signType': 'MD5',
          'paySign': rps.data.data.paySign,
          'success': function (res) {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000
            })
            pagethis.bindData()
          },
          'fail': function (rqs) {
            wx.showModal({
              title: '支付失败',
              content: rqs.data.msg,
              success: function (rqs) {

              }
            })
          }
        })
      }
    })
  },
  // 退单
  refundClick: function (event) {
    var pagethis = this
    wx.showModal({
      title: '退单',
      content: '确定退单？',
      success: function (rps) {
        if (rps.confirm) {
          Api.refundOrder({
            data: app.addParams({
              orderId: event.target.dataset.id
            }),
            success: (res) => {
              wx.showToast({
                title: '已完成',
                icon: 'success',
                duration: 2000
              });
              pagethis.bindData()
            }
          })
        }

      }
    })
  },
  bindData: function (e) {
    var pagethis = this
    Api.getOrderDetail({
      data:app.addParams({
        orderId: pagethis.data.orderId
      }),
      success:(res) => {
        console.log('+++++++++++++++++++下单页数据+++++++++++++++++++')
        console.log(res)
        let _items = res.data.data.orderDto;
        _items.payStatusName = Util.getOrderStatus(_items.payStatus);
        _items.createTime = Util.formatTime(new Date(_items.createTime));
        _items.occDate = Util.formatTime(new Date(_items.occDate))
        pagethis.setData({
          items: _items,
          orderRetreat: res.data.data.orderRetreat
        })
      }
    })
  }

})
















